import os.path
from typing import Tuple, List

import gradio as gr

from conf.config import BASE_DIR


def get_answer(model_name: str, platform_name: str, star_name: str, question: str) -> Tuple[str, List]:
    """
    获取答复
    :param model_name: 模型名称
    :param platform_name: 平台名称
    :param star_name: 达人名称
    :param question: 问题
    :return:
    """
    answer = "某某答复"
    answer_details = [
        ["aaa", "https://www.baidu.com"],
        ["bbb", "https://www.douyin.com"],
    ]

    return answer, answer_details


def main():
    with gr.Blocks(theme=gr.themes.Base(), title="ChatStar") as app:
        gr.Markdown("# <center>ChatStar")

        # 输入组件
        ele_dropdown_model = gr.Dropdown(
            ["gpt-3.5"], value="gpt-3.5", label="选择一个模型", info="目前只支持gpt-3.5, 后续支持ChatGLM-6B等"
        )
        with gr.Group():
            ele_radio_platform = gr.Radio(["抖音"], value="抖音", label="选择一个平台", info="目前只支持抖音")
            ele_dropdown_star = gr.Dropdown(
                ["大胡子说房"], value="大胡子说房", label="选择一个达人", info="目前只支持大胡子说房"
            )

        ele_text_box_question = gr.Textbox(label="你想问什么", placeholder="请输入你的问题", lines=3)

        # 输入示例
        gr.Examples(
            label="输入示例，点击可快速填充",
            examples=[
                ["gpt-3.5", "抖音", "大胡子说房", "今年适不适合买房？"],
                ["gpt-3.5", "抖音", "大胡子说房", "武汉未来的发展如何？"]
            ],
            inputs=[
                ele_dropdown_model, ele_radio_platform,
                ele_dropdown_star, ele_text_box_question
            ]
        )

        # 动作按钮
        ele_button = gr.Button("提交", variant="primary")

        # 输出组件
        ele_text_box_answer = gr.Textbox(label="答复（仅供参考）", lines=1)

        ele_dataframe_answer_details = gr.Dataframe(
            label="答复的详情",
            headers=["索引到的内容片段", "内容来源"],
            datatype=["str", "str"],
            row_count=(1, "dynamic"),
            col_count=(2, "fixed"),
        )

        # 事件绑定
        ele_button.click(fn=get_answer,
                         inputs=[
                             ele_dropdown_model,
                             ele_radio_platform,
                             ele_dropdown_star,
                             ele_text_box_question,
                         ],
                         outputs=[ele_text_box_answer, ele_dataframe_answer_details, ])

        app.queue(concurrency_count=10)

        app.launch(show_api=False, server_name="0.0.0.0", server_port=9098,
                   favicon_path=os.path.join(BASE_DIR, "logo.png"))


if __name__ == '__main__':
    main()
